﻿using IOP.Extension.DependencyInjection;
using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample1212;

public class TransformSystem : ComponentSystem
{
    private float AngleX;
    private float AngleY;
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="contextManager"></param>
    public TransformSystem()
    {
    }

    public override void Initialization()
    {
        base.Initialization();
    }

    public override void Update(TimeSpan timeSpan)
    {
        float diffX = 0;
        float diffY = 0;
        if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Press)
        {
            Vector2 p = Input.GetMousePositionDelta();
            diffX = p.X;
            diffY = p.Y;
        }
        AngleX += diffX * 180 / 200;
        AngleY += diffY * 180 / 200;
        ContextManager.Foreach<OBJGroup>((group) =>
        {
            Context context = ContextManager.GetContext(group.Entity);
            ref MVPMatrix mvp = ref context.GetMVPMatrix();
            var render = context.GetContextRenderObject();
            mvp.ModelMatrix = Matrix4x4.CreateRotationY(AngleX / 180.0f * MathF.PI) *
            Matrix4x4.CreateRotationX(AngleY / 180.0f * MathF.PI) * Matrix4x4.CreateTranslation(render.GetTransform().Position);
            context.PushToRenderGroup();
        });
    }
}

public struct OBJGroup
{
    public Entity Entity;
    public TransformComponent Transform;
}
